import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:tracker/pages/home/home/view/home_page.dart';
import 'package:tracker/pages/msg/view/msg_page.dart';
import 'package:tracker/pages/profile/view/profile_page.dart';
import 'package:tracker/router/router_manager.dart';
import 'package:tracker/theme/app_theme.dart';

class BottomNavigatorPage extends ConsumerStatefulWidget {
  const BottomNavigatorPage({super.key});

  @override
  ConsumerState<BottomNavigatorPage> createState() => _BottomNavigatorPageState();
}

class _BottomNavigatorPageState extends ConsumerState<BottomNavigatorPage> {
  int _currentIndex = 0;
  final _pageController = PageController(
    initialPage: 0,
  );
  double _tabbarSize = 24;

  final List<Widget> _pages = [const HomePage(), const MsgPage(), const ProfilePage()];

  /// 根据页面地址打开页面
  _openPageByPath(BuildContext context, String path) {
    if (path.startsWith('http')) {
      context.push('${RouterManager.web}?url=${Uri.encodeComponent(path)}');
    } else {
      context.push(path);
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: _pages,
      ),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.08),
              blurRadius: 12,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            _pageController.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          selectedItemColor: AppTheme.themeConfig.primaryColor,
          unselectedItemColor: AppTheme.themeConfig.infoTextColor,
          selectedFontSize: 13,
          unselectedFontSize: 12,
          type: BottomNavigationBarType.fixed,
          elevation: 0,
          backgroundColor: Colors.transparent,
          selectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600, letterSpacing: 0.3),
          unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.w500),
        items: [
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/home/XJTabbarHomeN.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              activeIcon: Image.asset(
                'assets/images/home/XJTabbarHomeY.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              label: '首页'),
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/home/XJTabarXiaoxiN.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              activeIcon: Image.asset(
                'assets/images/home/XJTabbarXiaoxiY.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              label: '消息'),
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/home/XJTabbarMIneN.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              activeIcon: Image.asset(
                'assets/images/home/XJTabbarMineY.png',
                width: _tabbarSize,
                height: _tabbarSize,
              ),
              label: '我的'),
          ],
        ),
      ),
    );
  }
}
